// 路径解析
// 成语接龙
// 等式成立
// add[1][2][3] proxy 　[]是get 的用法



// 路径解析
function findAllPaths(arr, start, end) {
    const graph = {}
    arr.forEach(item => {
      const [start, end] = item.split('-');
      if(!graph[start]) graph[start] = []
      graph[start].push(end)
    })

    const paths = []
    const visited = new Set()

    function dfs(currentNode, path) {
        if(currentNode === end){
          paths.push(path) // 找到一条路径
          return
        }
        visited.add(currentNode) // 标记当前搜索
        for(let i = 0; i< graph[currentNode]?.length || [].length; i++){
          if(!visited.has(graph[currentNode][i])){
            dfs(graph[currentNode][i], path.concat(graph[currentNode][i])) // 
          }
        }
        visited.delete(currentNode) // 衣橱当前标记
    }
    dfs(start, [start])
    return paths

}// 使用示例
const arr = ['a-b', 'b-c', 'a-d', 'c-d', 'd-f'];
const start = 'a';
const end = 'f';
const allPaths = findAllPaths(arr, start, end);
console.log(allPaths);


function findLongestIdiomChain(arr) {
  const graph = {}
  arr.forEach(item => {
    const start = item[0]
    if (!graph[start]) graph[start] = []
    graph[start].push(item)
  })

  let res = 0
  for (let i = 0; i < arr.length; i++) {
    const visited = new Set()
    let paths = dfs([], arr[i], visited)

    res = Math.max(paths.length, res)
  }

  function dfs(temp, currentNode, visited) {
    visited.add(currentNode) // 标记当前搜索
    let key = currentNode[currentNode.length - 1]
    temp.push(currentNode)
    for (let i = 0; i < graph[key]?.length || [].length; i++) {
      if (!visited.has(graph[key][i])) {
        let paths = dfs(temp.slice(), graph[key][i], visited) // 
        if(paths.length > temp.length){
          temp = paths
        }
      }
    }
    visited.delete(currentNode) // 衣橱当前标记
    return temp
  }

  return res
}
// 示例用法
const idioms = ["接踵而来", "来日方长", "长生久视", "视死如归", "归心似箭", "1箭在弦上"];
console.log(findLongestIdiomChain(idioms), '成语接龙'); // 输出最长长度



// 等式成立
var a = {
  n: 1,
  valueOf: function () {
    return this.n++
  }
}
console.log(a == 1 && a == 2 && a == 3)


// add[1][2][3] proxy 　[]是get 的用法
const add = new Proxy({ value: 0 }, {
  get(target, key) {
    if (key === Symbol.toPrimitive) {
      return () => target.value
    }
    let newVal = target.value + Number(key)
    return new Proxy({ value: newVal }, this)
  }
})
console.log(add[1][2][3])



// class 转 es5
// 1. 严格模式 2. 不能直接调用 3. 挂载方法不能枚举 4. 挂载方法不能直接new
'use strict'
function Example(name){
    if(!new.target){ // 不能直接调用
        throw new TypeError('type error')
    }
    this.name = name
}

// Example.prototype.func = fucntion() {
//     console.log(this.name);
// }
Object.defineProperty(Example.prototype, 'func', {
    value: function(){
        if(!new.target){ // 不能直接调用
            throw new TypeError('type error')
        }
        console.log(this.name)
    },
    enumerable: false
})

// Example() 不可以
// new Example.prototype.func()
// const = new Example()